<template>
  <view>
    <!-- 公共组件-每个页面必须引入 -->
    <public-module></public-module>
		<view class="account-form-box" v-if="notj_show==false">

				<u--form labelPosition="top"  label-width="80">
				<u-form-item class="form-item" label="我的推荐人">
				  <u-radio-group v-model="upnode_user" placement="column" @change="radioGroupChange">
					<u-radio
						@change="radioChange" v-for="(item, index) in upnode_user_tree"
						:label="item.nickname"
						:key="index"
						:labelSize="16" :size="16"
						:customStyle="{marginBottom: '15px'}"
						:name="index">
						<!--<text class="u-p-r-20 u-type-success-dark" v-if="item.left_user.enable==1">Left</text>
						<text class="u-p-r-20 u-type-info-disabled" v-else>Right</text>
						<text class="u-p-r-20 u-type-success-dark" v-if="item.right_user.enable==1">Left</text>
						<text class="u-p-r-20 u-type-info-disabled" v-else>Right</text> -->
					</u-radio>
				  </u-radio-group>
				</u-form-item>
				
				<u-form-item label="位置">
					<u-radio-group v-model="position_value" @change="l_r_Change"  placement="row">
						<u-radio
							v-for="(item, index) in position" :key="index" 
							:name="item.value"
							:label="item.name"
							:customStyle="{marginRight: '45px'}"
							:disabled="item.disabled">
						</u-radio>
					</u-radio-group>
				</u-form-item>
			</u--form>

			<u-gap height="20"></u-gap>
			<u-button type="primary" @click="submit">加入团队</u-button>



		</view>

			
				<u-popup :show="notj_show" mode="bottom" @close="close_notj()" :round="10" safeAreaInsetBottom >
				<view class="pops u-padding-20">
					<u--form ref="uForm" labelPosition="left"  label-width="80">
						<u-form-item class="form-item" label="推荐码">
						  <u-input placeholder="请填写一个推荐码" v-model="salt_parentcode" type="text"/>
						  <!-- <text slot="right" style="font-size: 22rpx">USD</text> -->
						</u-form-item>
					</u--form>
					<u-gap height="20"></u-gap>
					<u-button type="primary" @click="add_parentcode">确认</u-button>
					<u-gap height="40"></u-gap>
				</view>
				</u-popup>
		
		
	</view>
</template>

<script>
import {mapState,mapMutations} from 'vuex';
export default {
	data() {
		return {
			codeTips: '',
			upnode_user_tree:[],
			upnode_user:null,
			upnode_user_id:'',
			position:[
				{
					name: 'L',
					value:1,
					disabled: true
				},
				{
					name: 'R',
					value:3,
					disabled: true
				}
			],
			position_value: '',
			notj_show:false,
			code:'',
			salt_parentcode:''

		};
	},
	computed: {
		...mapState(['user'])
	},
	onLoad(options) {

		let query=this.user.chatScenesInfo.query;
		if (query == null || query=="" || query==undefined){
			
		}else{
			let querys=JSON.parse(decodeURIComponent(query));
			this.salt_parentcode=querys.s	
		}
		console.log(this.user.userInfo)
		this.notjshow(this.user.userInfo.parent_id)

		}, 
	methods: {
	  close_notj(){
		  uni.navigateBack(1);
		  if (this.code==202){
			  uni.navigateBack(1);
		  }
	  },
		// 选中某个单选框时，由radio时触发
		radioChange(e) {
			 //let selectnode_param=this.upnode_user_tree[e];
			 //console.log(selectnode_param)
		},
		// 选中任一radio时，由radio-group触发
		radioGroupChange(e) {
			 let selectnode_param=this.upnode_user_tree[e];
			 this.upnode_user_id=selectnode_param.node_id
			 if (selectnode_param.left_user.enable==0){this.position[0].disabled=true}
			 if (selectnode_param.left_user.enable==1){this.position[0].disabled=false}
			 
			 if (selectnode_param.right_user.enable==0){this.position[1].disabled=true}
			 if (selectnode_param.right_user.enable==1){this.position[1].disabled=false}
			 
			 
			 console.log(selectnode_param)
		},
		l_r_Change(e){
			console.log(e)
			this.position_value=e
		},
		submit() {
                    this.$api('user.addnode', {
						upnode_user_id:this.upnode_user_id,
						position_value:this.position_value
					}).then(res => {
						console.log(res);
						if (res.code==200){
							this.$store.commit('USER_INFO',res.data);
							this.$tools.msg("成功加入");
							setTimeout(() => {
								this.ineeds_show=false;
								  uni.switchTab({url: `/pages/index/my`,success: function (e) {}})
							}, 2000);
						}
                    }).catch((e)=>{});
		},
		add_parentcode(){
                    this.$api('user.addparentcode', {
						user_salt_parentcode:this.salt_parentcode
					}).then(res => {
						console.log(res);
						if (res.code==200){
							this.$store.dispatch("getUserInfo");
							this.notj_show=false
							this.getnode(res.data.uid)
						}
                    }).catch((e)=>{});
		},
	notjshow(parent_id){
		this.notj_show=true;return;
	},
    getnode(parent_id) {
		//find_users_id: this.user.userInfo.parent_id
		if (parent_id==0||parent_id==''){
			//this.$tools.msg("无推荐人");return;
			this.notj_show=true;return;
		}
        this.$api("user.team_nodetree", { 
          find_users_id: parent_id
        }).then((res) => {
			console.log(res)
            if (res.code == 200) {
				this.upnode_user_tree=res.data
            }
          }).catch((e) => {});

    },
		codeChange(text) {
			this.codeTips = text;
		},
	}
};
</script>

<style lang="scss" scoped>
.account-form-box {
	padding: 0 32rpx;
}
</style>
